package com.lvshi0797.cms.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lvshi0797.cms.dao.IBaseInfoDao;
import com.lvshi0797.cms.dao.IProjectInfoDao;
import com.lvshi0797.cms.dto.ProjectDto;
import com.lvshi0797.cms.entities.FBaseInfo;
import com.lvshi0797.cms.entities.FManager;
import com.lvshi0797.cms.entities.FProjectInfo;
import com.lvshi0797.cms.entities.Pager;
import com.lvshi0797.cms.helper.BussinessException;
import com.lvshi0797.cms.helper.DateUtils;

@Service("baseInfoService")
public class BaseInfoService implements IBaseInfoService {
	@Autowired
	private IBaseInfoDao baseInfoDao;
	@Autowired
	private IProjectInfoDao projectInfoDao;

	@Override
	public FBaseInfo createArchive(FBaseInfo baseInfo)
			throws BussinessException {
		if (baseInfo == null || baseInfo.getManager() == null) {
			throw new BussinessException("请求数据异常，请稍后重新登录提交！");
		}
		baseInfo.setAddTime(new Date());// 默认添加时间
		FBaseInfo add = baseInfoDao.add(baseInfo);
		return add;
	}

	@Override
	public List<FBaseInfo> listBaseInfo(int id) {
		return baseInfoDao.listBaseInfoByMid(id);
	}

	@Override
	public Pager<FBaseInfo> findBaseInfo(int id) {
		return baseInfoDao.findBaseInfoByMid(id);
	}

	@Override
	public void updateBaseinfo(FBaseInfo baseInfo) throws BussinessException {
		if (baseInfo == null || baseInfo.getId() <= 0) {
			throw new BussinessException("当前业务信息不存在！");
		}
		// FBaseInfo fBaseInfo = baseInfoDao.load(baseInfo.getId());
		// if(fBaseInfo==null){
		// throw new BussinessException("当前业务信息不存在！");
		// }
		// baseInfo.setAddTime(fBaseInfo.getAddTime());
		// baseInfo.setManager(fBaseInfo.getManager());
		baseInfoDao.updateBaseInfo(baseInfo);
	}

	@Override
	public FBaseInfo loadBaseInfo(int id) throws BussinessException {
		FBaseInfo baseInfo = baseInfoDao.load(id);
		if (baseInfo == null) {
			throw new BussinessException("暂无此数据！");
		}
		// baseInfo.setMembers(memberDao.findMembersBybaseId(id));
		// baseInfo.setProjects(projectInfoDao.findProjectInfoBybaseId(id));
		return baseInfo;
	}

	@Override
	public List<FBaseInfo> listBaseInfo(FManager manager, String huzhuName,
			String town, String isOutpoor, String startD, String endD)
			throws BussinessException {
		List<FBaseInfo> list = new ArrayList<FBaseInfo>();
		if (manager == null) {
			throw new BussinessException("用户账户不存在，请重新登录！");
		}
		int mid = manager.getId();
		if (manager.getIsAdmin() == 1) {// 超级管理员的话查出全部的
			list = baseInfoDao.listBaseInfo(0, huzhuName, town,
					Integer.valueOf(isOutpoor), startD, endD);
		} else {
			list = baseInfoDao.listBaseInfo(mid, huzhuName, town,
					Integer.valueOf(isOutpoor), startD, endD);
		}
		return list;
	}

	@Override
	public Pager<FBaseInfo> findBaseInfo(FManager manager, String huzhuName,
			String town, String isOutpoor, String addTime)
			throws BussinessException {
		Pager<FBaseInfo> pager = new Pager<FBaseInfo>();
		if (manager == null) {
			throw new BussinessException("用户账户不存在，请重新登录！");
		}
		int mid = manager.getId();
		// 对这三个变量进行初始化操作
		/**
		 * <option value="0">全部</option> <option value="1">七天内</option> <option
		 * value="2">一个月内</option> <option value="3">近三个月</option> <option
		 * value="4">本年度</option>
		 */
		String startD = null;
		String endD = null;
		if (addTime != null) {
			Calendar cal = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			endD = sdf.format(cal.getTime());
			if ("1".equals(addTime)) {
				startD = sdf.format(DateUtils.getWeekFromNow());
			} else if ("2".equals(addTime)) {
				startD = sdf.format(DateUtils.getMonthFromNow());
			} else if ("3".equals(addTime)) {
				startD = sdf.format(DateUtils.get3MonthFromNow());
			} else if ("4".equals(addTime)) {
				startD = sdf.format(DateUtils.getCurrentYearStartTime());
			}
		}
		int outflag = -1;
		if (isOutpoor != null && StringUtils.isNotEmpty(isOutpoor)) {
			outflag = Integer.valueOf(isOutpoor);
		}

		if (manager.getIsAdmin() == 1) {// 超级管理员的话查出全部的
			pager = baseInfoDao.findBaseInfo(0, huzhuName, town, outflag,
					startD, endD);
		} else {
			pager = baseInfoDao.findBaseInfo(mid, huzhuName, town, outflag,
					startD, endD);
		}
		return pager;
	}

	@Override
	public void deleteBaseInfoById(int id) throws BussinessException {
		baseInfoDao.delete(id);
	}

	@Override
	public void clearProject(int baseId, String niandu) {
		projectInfoDao.clearProjectsByBaseId(baseId, niandu);
	}

	@Override
	public Set<Integer> addProjects(ProjectDto pdo, int baseId) {
		List<FProjectInfo> projects = pdo.getProjects();
		// 先删除已有的baseId名下某年度所有的项目信息
		String niandu = pdo.getNiandu();
		if (StringUtils.isNotEmpty(niandu)) {
			clearProject(baseId, niandu);
		}
		Set<Integer> ids = new HashSet<Integer>();
		for (FProjectInfo project : projects) {
			FProjectInfo add = projectInfoDao.add(project);
			ids.add(add.getId());
		}
		return ids;
	}

	@Override
	public ProjectDto getProjects(int baseId, String niandu) {
		ProjectDto pdo = new ProjectDto();
		List<FProjectInfo> projectInfo = projectInfoDao.listProjectInfoBybaseId(
				baseId, niandu);
		pdo.setProjects(projectInfo);
		return pdo;
	}

	@Override
	public void updateProject(Set<Integer> ids, int baseId) {
		projectInfoDao.updateProject(ids, baseId);
	}

}
